package ru.spbu.math.android.cbrf.application;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.dataprovider.ProviderManager;
import ru.spbu.math.android.cbrf.model.RateType;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;

public class FavoriteListAdapter implements ListAdapter {
	
	private static final String LOG_TAG = "Favorite List Adapter";

	List<RateType> favoriteList;
	Map<RateType, View> views;
	Context context;
	
	protected FavoriteListAdapter(Context context, List<RateType> listRate){
		this.context = context;
		this.favoriteList = listRate;
		
		views = new HashMap<RateType, View>();
		Log.d(LOG_TAG, "FavoriteList size : " + favoriteList.size());
		for(RateType rateType : favoriteList){
			views.put(rateType, makeViews(rateType));
		}
		
	}
	
	@Override
	public int getCount() {
		return favoriteList.size();
	}

	
	@Override
	public Object getItem(int position) {
		return favoriteList.get(position);
	}

	
	@Override
	public long getItemId(int position) {
		return position;
	}

	
	@Override
	public int getItemViewType(int position) {
		return IGNORE_ITEM_VIEW_TYPE;
	}

	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return views.get(favoriteList.get(position));
	}

	
	@Override
	public int getViewTypeCount() {
		return 1;
	}

	
	@Override
	public boolean hasStableIds() {
		return false;
	}

	
	@Override
	public boolean isEmpty() {
		return favoriteList.isEmpty();
	}

	
	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
	}

	
	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
	}

	
	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	
	@Override
	public boolean isEnabled(int position) {
		return true;
	}
	
	private View makeViews(RateType rateType){
		View res = LayoutInflater.from(context).inflate(
				R.layout.favorite_list_item, null);
		ProviderManager providerManager = ProviderManager.getInstance(context);
		((TextView) res.findViewById(R.id.toptext)).setText(rateType.getName());
		((TextView) res.findViewById(R.id.bottomtext)).setText(providerManager.getCurrentRate(providerManager.getExternalIdByRateType(rateType)).getValue() + "");

		return res;
	}

}
